package com.arcbbs.controller;

import com.arcbbs.annotation.GlobalInterceptor;
import com.arcbbs.constant.BaseConstant;
import com.arcbbs.entity.SessionWebUserDto;
import com.arcbbs.entity.SysSettingDto;
import com.arcbbs.entity.SystemSettingCommentDto;
import com.arcbbs.exception.BaseException;
import com.arcbbs.result.Result;
import com.arcbbs.service.EmailCodeService;
import com.arcbbs.service.UserInfoService;
import com.arcbbs.utils.CreateImageCode;
import com.arcbbs.utils.StringTools;
import com.arcbbs.utils.SysCacheUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AccountController {
    @Resource
    private EmailCodeService emailCodeService;
    @Resource
    private UserInfoService userInfoService;

    @GetMapping("/checkcode")
    public void checkcode(HttpServletResponse response, HttpSession session, Integer type) throws IOException {
        CreateImageCode vcode = new CreateImageCode(130, 38, 4);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        //生效时间
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String code = vcode.getCode();

        if (type == null || type == 0) {
            //登录注册
            session.setAttribute(BaseConstant.CHECK_CODE_KEY, code);
            System.out.println(code + "注册");
        } else {
            session.setAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL, code);
            System.out.println(code + "邮箱");

        }
        System.out.println((String) session.getAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL));

        vcode.write(response.getOutputStream());

    }
    @CrossOrigin
    @GetMapping("/sendEmailCode")
    public Result sendEmailCode(HttpSession httpSession, String email, String checkCode, Integer type) {
        System.out.println("begin");
        try {
            if (StringTools.isEmpty(email) || StringTools.isEmpty(checkCode) || type == null) {
                throw new BaseException("参数错误");
            }
            System.out.println(checkCode);
            System.out.println((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL)+"email");
            System.out.println((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY)+"zhuce");
            if (!checkCode.equalsIgnoreCase((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL))) {
                throw new BaseException("图片验证码错误");

            }
            emailCodeService.sendEmailCode(email, type);

            return Result.success();
        } finally {
            System.out.println("finally");
            httpSession.removeAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL);

        }


    }

    @RequestMapping("/login")
    @GlobalInterceptor(checkparams = true)
    public Result login(HttpSession httpSession,String email,String password, String checkcode){
try {
    System.out.println(httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY)+"登录验证码");
    if (!checkcode.equalsIgnoreCase((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY))) {
        throw new BaseException("图片验证码错误");

    }
    String ip="";
    SessionWebUserDto sessionWebUserDto=userInfoService.login(email,password,ip);
    httpSession.setAttribute("sessionKey",sessionWebUserDto);
    return Result.success(sessionWebUserDto);
}finally {
    httpSession.removeAttribute(BaseConstant.CHECK_CODE_KEY);
}
    }

    @RequestMapping("/register")
    @GlobalInterceptor(checkparams = true)
    public Result register(HttpSession httpSession, String email, String emailCode, String nickName, String password, String checkcode) {
        try {
//            if (StringTools.isEmpty(email) || StringTools.isEmpty(emailCode) || StringTools.isEmpty(nickName) || StringTools.isEmpty(password)
//                    || StringTools.isEmpty(checkcode)
//            ) {
//                throw new BaseException("参数错误");
//
//            }
            if (!checkcode.equalsIgnoreCase((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY))) {
                throw new BaseException("图片验证码错误");

            }
            userInfoService.register( email,  emailCode,  nickName,  password);
            return Result.success("成功");

        } finally {
            httpSession.removeAttribute(BaseConstant.CHECK_CODE_KEY);

        }

    }
    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpSession httpSession){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute("sessionKey");
return Result.success(sessionWebUserDto);
    }
    @GetMapping("/logout")
    public Result logout(HttpSession httpSession){
        httpSession.removeAttribute("sessionKey");
        return Result.success();
    }
    @GetMapping("/getSysSetting")
    public Result getSysSetting(){
        SysSettingDto sysSettingDto= SysCacheUtils.getSysSetting();
        SystemSettingCommentDto systemSettingCommentDto=sysSettingDto.getSystemSettingCommentDto();
        Map<String,Object> map=new HashMap<>();
        map.put("commentOpen",systemSettingCommentDto.getCommentOpen());
        return Result.success(map);
    }
    @RequestMapping("/resetpwd")
    public Result resetpwd(HttpSession httpSession,String email,String password,String checkcode,String emailcode){
        try {
            if (!checkcode.equalsIgnoreCase((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY))) {
                throw new BaseException("图片验证码错误");

            }
            userInfoService.resetpwd(email,password,emailcode,checkcode);
            return Result.success();
        }finally {
            httpSession.removeAttribute(BaseConstant.CHECK_CODE_KEY);
        }

    }
}
